﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Leikjavefurinn.Context;

namespace Leikjavefurinn.Repo
{
    public class TicTacToeRepo : ITicTacToeRepo
    {
        LeikjavefurEntities db = new LeikjavefurEntities();
        UserExtensionRepo userExtentionRepo = new UserExtensionRepo();
        ScoreboardRepo scoreBoardRepo = new ScoreboardRepo();

        public IEnumerable<GameStatus> AllGames()
        {
            return (from g in db.GameStatus select g).ToList();
        }

        public IEnumerable<TicTacToeGame> AllTickTacToe()
        {
            return (from t in db.TicTacToeGame select t).ToList();
        }

        public int CreateGame(String name, Users x) // Það ætti í raun bara að vera einn user sem kemur inn (sem býr til leikinn svo joinar hinn)
        {
            TicTacToeGame game = new TicTacToeGame();

            // Þetta var bara meðan við vorum að testa - ætti ekki að vera svona :)
            //var users = db.Users.Take(2).ToArray();
            //game.Users = users[1];

            game.UserX = x.UserId; // Ætti að setja bara inn userinn sem kemur inn sem parameter ??? svona ?

            game.GameName = name;
            db.TicTacToeGame.Add(game);
            db.SaveChanges();

            var gameStatus = new GameStatus()
            {
                GameName = name,
                IsActive = true,
                GameType = 0,
                IsDone = false,
                IsWaiting = false,
                Users = x
            };
            db.GameStatus.Add(gameStatus);
            db.SaveChanges();

            return game.GameId;
        }

        //Næ í Leik í gegnum GameId 
        public TicTacToeGame GetGame(int id)
        {
            return db.TicTacToeGame.SingleOrDefault(n => n.GameId == id);
        }

        //Nær í user í gegnum User.Identity.Name
        public Users GetUser(string userName)
        {
            return db.Users.SingleOrDefault(n => n.UserName == userName);
        }

        //Nær í notenda af handahófi, við notuðum þetta til að setja 
        public Users GetRandomUser(string userName)
        {
            var users = (from r in db.Users
                         select r).ToList();
            int count = users.Count();
            var randomUsers = new System.Random(DateTime.Now.Millisecond);
            var index = randomUsers.Next(0, count - 1);
            return users[index];
        }

        public void SetWinnerID(Guid id)
        {
            TicTacToeGame gameWinner = new TicTacToeGame();
            gameWinner.WinnerId = id;
            db.SaveChanges(); //WinnerId vistast ekki

            AddPointToScoreByID(id); //Þetta virkar
            Save();
        }

        public void AddPointToScoreByID(Guid id)
        {
            UserExtention thisUser = userExtentionRepo.GetUserExtension(id);
            thisUser.Score += 1;
            userExtentionRepo.Save();
            //Læt ScoreBoard hafa jafn mörg stig og UserExtention
            Scoreboard thisScoreBoard = scoreBoardRepo.GetScoreBoard(id);
            thisScoreBoard.Score = thisUser.Score;
            scoreBoardRepo.Save();
        }

        public void AddPointToScoreByName(string userName)
        {
            //Bæti við einu stigi í UserExtention töfluna
            UserExtention thisUser = userExtentionRepo.GetUserExtension(userName);
            thisUser.Score += 1;
            userExtentionRepo.Save();
            //Læt ScoreBoard hafa jafn mörg stig og UserExtention
            Scoreboard thisScoreBoard = scoreBoardRepo.GetScoreBoard(userName);
            thisScoreBoard.Score = thisUser.Score;
            scoreBoardRepo.Save();
        }

        public void AddPointToRate(string userName)
        {
            //Bæti við einu rate í UserExtention töfluna
            UserExtention thisUser = userExtentionRepo.GetUserExtension(userName);
            thisUser.Rate += 1;
            userExtentionRepo.Save();
            //Læt ScoreBoard hafa jafn mikið rate og UserExtention
            Scoreboard thisScoreBoard = scoreBoardRepo.GetScoreBoard(userName);
            thisScoreBoard.Rate = thisUser.Rate;
            scoreBoardRepo.Save();
        }

        public void DetractPointFromRate(string userName)
        {
            //Minnka rate um eitt í UserExtention töflunni
            UserExtention thisUser = userExtentionRepo.GetUserExtension(userName);
            thisUser.Rate -= 1;
            userExtentionRepo.Save();
            //Læt ScoreBoard hafa jafn mikið rate og UserExtention	
            Scoreboard thisScoreBoard = scoreBoardRepo.GetScoreBoard(userName);
            thisScoreBoard.Rate = thisUser.Rate;
            scoreBoardRepo.Save();
        }

		public void Save()
		{
			db.SaveChanges();
		}
	}
}